package com.ruoyi.peach.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.peach.domain.CollectRecord;
import com.ruoyi.peach.service.ICollectRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采集记录Controller
 * 
 * @author ruoyi
 * @date 2025-09-22
 */
@RestController
@RequestMapping("/peach/collectRecord")
public class CollectRecordController extends BaseController
{
    @Autowired
    private ICollectRecordService collectRecordService;

    /**
     * 查询采集记录列表
     */
    @PreAuthorize("@ss.hasPermi('peach:collectRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollectRecord collectRecord)
    {
        startPage();
        List<CollectRecord> list = collectRecordService.selectCollectRecordList(collectRecord);
        return getDataTable(list);
    }
    /**
     * 查询采集记录列表
     */
    @PreAuthorize("@ss.hasPermi('peach:collectRecord:list')")
    @GetMapping("/list/app")
    public R listApp(CollectRecord collectRecord)
    {
        List<CollectRecord> list = collectRecordService.selectCollectRecordList(collectRecord);
        return R.ok(list);
    }

    /**
     * 导出采集记录列表
     */
    @PreAuthorize("@ss.hasPermi('peach:collectRecord:export')")
    @Log(title = "采集记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "recordIds", required = false) String recordIds,
                       CollectRecord collectRecord)
    {
        List<CollectRecord> list;

        if (recordIds != null && !recordIds.trim().isEmpty()) {
            // 按照勾选的recordId导出
            Long[] recordIdArray = Arrays.stream(recordIds.split(","))
                    .map(Long::valueOf)
                    .toArray(Long[]::new);
            list = collectRecordService.selectCollectRecordListByIds(recordIdArray);
        } else {
            list = collectRecordService.selectCollectRecordList(collectRecord);
        }

        ExcelUtil<CollectRecord> util = new ExcelUtil<CollectRecord>(CollectRecord.class);
        util.exportExcel(response, list, "采集记录数据");
    }

    /**
     * 获取采集记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('peach:collectRecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(collectRecordService.selectCollectRecordByRecordId(recordId));
    }

    /**
     * 新增采集记录
     */
    @PreAuthorize("@ss.hasPermi('peach:collectRecord:add')")
    @Log(title = "采集记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CollectRecord collectRecord)
    {
        return toAjax(collectRecordService.insertCollectRecord(collectRecord));
    }

    /**
     * 修改采集记录
     */
    @PreAuthorize("@ss.hasPermi('peach:collectRecord:edit')")
    @Log(title = "采集记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CollectRecord collectRecord)
    {
        return toAjax(collectRecordService.updateCollectRecord(collectRecord));
    }

    /**
     * 删除采集记录
     */
    @PreAuthorize("@ss.hasPermi('peach:collectRecord:remove')")
    @Log(title = "采集记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(collectRecordService.deleteCollectRecordByRecordIds(recordIds));
    }
}
